'''
Created on Jun 13, 2013

@author: andrea
'''
class Tape:
        RIGHT = +1
        LEFT = -1
        NO_MOVE = 0
        """A unidimensional infinite tape of characters with a head"""
        def __str__(self):
                """The content of the tape, with the current position highlighted by vertical bars"""
                return ''.join(self.leftList + ['|', self.currentValue, '|'] + self.rightList[::-1])
       
        def __init__(self, blank, content ):
                """Construct a new tape with the specified content and the default value elsewhere"""
                self.leftList, self.currentValue, self.rightList = content.split('|')
                self.leftList, self.rightList = list(self.leftList), list(self.rightList[::-1])
                self.blankValue = blank
        
        def move(self, step):
                """Move the tape"""
                if step == Tape.NO_MOVE: # do not move
                        pass
                elif step in [Tape.LEFT, Tape.RIGHT]: # move left or right
                        toAppend, toPop = (self.leftList, self.rightList) if step==Tape.RIGHT else (self.rightList, self.leftList)
                        toAppend.append(self.currentValue) # append the current state to a lateral list
                        try:
                                self.currentValue = toPop.pop() # update the current position
                        except IndexError:
                                self.currentValue = self.blankValue # if the list is empty, use the default value
                else: # invalid move
                        raise ValueError('the step {} is not a valid value'.format(type(step)))